using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;
using System.Web.Configuration;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Collections.Generic;
namespace eGov.SharePoint.MyPeeps.WebParts
{
    [Guid("e5baef73-8538-4ae1-9b14-c1d24bcdd615")]
    public class WhatsOnYourMind : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _peepSite = null;
        private string _peepUser = null;
        private string _peepPwd = null;
        private string _fbSessionKey = null;
        private long _fbUserID = -1;
        private string _CSP = "~/_controltemplates/eGov/WhatsOnYourMind.ascx";
        protected static string PROPERTY_EXTERNALUPDATES_KEY = WebConfigurationManager.AppSettings["WhatsOnYourMindExternalUpdates"];
        protected static bool PROPERTY_EXTERNALUPDATES
        {
            get
            {
                if (String.IsNullOrEmpty(PROPERTY_EXTERNALUPDATES_KEY)) return false;
                if (String.Compare(PROPERTY_EXTERNALUPDATES_KEY, "true", true, CultureInfo.InvariantCulture) == 0 ||
                    String.Compare(PROPERTY_EXTERNALUPDATES_KEY, "1", true, CultureInfo.InvariantCulture) == 0) return true;
                return false;
            }
        }


        #region Properties

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(false)]
        public string PeepSite
        {
            get
            {
                if (_peepSite == null)
                {
                    _peepSite = Properties.Settings.Default.InitialPeepSite;
                }
                return _peepSite;
            }
            set { _peepSite = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(false)]
        public string PeepUser
        {
            get
            {
                if (_peepUser == null)
                {
                    _peepUser = Properties.Settings.Default.InitialPeepUser;
                }
                return _peepUser;
            }
            set { _peepUser = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(false)]
        public string PeepPassword
        {
            get
            {
                if (_peepPwd == null)
                {
                    _peepPwd = Properties.Settings.Default.InitialPeepSitePassword;
                }
                return _peepPwd;
            }
            set { _peepPwd = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(false)]
        [WebDisplayName("fbsession")]
        public string FBSessionKey
        {
            get { return _fbSessionKey; }
            set { _fbSessionKey = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(false)]
        [WebDisplayName("fbuserid")]
        public long FBUserID
        {
            get { return _fbUserID; }
            set { _fbUserID = value; }
        }

        #endregion


        public WhatsOnYourMind()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        protected eGov.SharePoint.MyPeeps.WebControls.WhatsOnYourMind woym = null;

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();
                    woym = (eGov.SharePoint.MyPeeps.WebControls.WhatsOnYourMind)this.Page.LoadControl(_CSP);
                    woym.SiteURL = PeepSite;
                    //woym.ListName = PeepList;
                    woym.User = PeepUser;
                    woym.Password = PeepPassword;
                    woym.FBSessionKey = this.FBSessionKey;
                    woym.FBUserID = this.FBUserID;
                    woym.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(WhatsOnYourMind_PropertyChanged);
                    //woym.OnShareButonClicked += new EventHandler(woym_ShareButonClicked);
                    //woym.Attributes.Add("OnShareButonClicked", "javascript:ReloadUpdates();");
                    this.Controls.Add(woym);

                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        protected void woym_ShareButonClicked(object sender, EventArgs e)
        {
            //RaiseBubbleEvent(sender, e);
        }

        protected void WhatsOnYourMind_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (woym != null)
            {
                switch (e.PropertyName)
                {
                    case "FBSessionKey": this.FBSessionKey = woym.FBSessionKey; break;
                    case "FBUserID": this.FBUserID = woym.FBUserID; break;
                }

                this.SaveProperties = true;
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }

        public override EditorPartCollection CreateEditorParts()
        {
            /// Skip twitter & facebook settings if web.config modification signals for disabled external updates
            if (PROPERTY_EXTERNALUPDATES == false)
                return base.CreateEditorParts();

            List<EditorPart> editorParts = new List<EditorPart>();
            EditorPart part = new WhatsOnYourMindWebPartEditor();
            part.ID = this.ID + "woym_editor";
            part.Title = "External Updates Settings";
            editorParts.Add(part);
            EditorPartCollection baseParts = base.CreateEditorParts();
            return new EditorPartCollection(baseParts, editorParts);

        }

        public void LogoutActiveFBSession()
        {
            if (this.woym != null)
                this.woym.LogoutActiveFBSession();
        }
    }
}
